import { get, post, put, del } from "./api.js";

export const companyJobService = {
  async getCompanyJobs(
    page = 1,
    pageSize = 10,
    searchType = "id",
    keyword = ""
  ) {
    try {
      const params = new URLSearchParams();
      params.append("page", page);
      params.append("pageSize", pageSize);
      params.append("searchType", searchType);
      params.append("keyword", keyword);

      const response = await get(`/company-jobs?${params.toString()}`);

      // 处理后端返回的数据格式
      const data = response.data || response;
      const items = Array.isArray(data) ? data : data.content || [];
      const total = data.totalElements || items.length;
      const totalPages = data.totalPages || Math.ceil(total / pageSize);

      return {
        items,
        total,
        totalPages,
        currentPage: page,
      };
    } catch (error) {
      console.error("Error fetching company jobs:", error);
      throw new Error("获取招聘信息列表失败");
    }
  },

  async getCompanyJob(id) {
    if (!id) throw new Error("招聘信息ID不能为空");
    try {
      const response = await get(`/company-jobs/${id}`);
      const data = response.data || response;
      if (!data) throw new Error("招聘信息不存在");
      return data;
    } catch (error) {
      console.error("Error fetching company job:", error);
      throw new Error("获取招聘信息失败");
    }
  },

  async createCompanyJob(data) {
    if (!data.jobTitle) throw new Error("招聘岗位不能为空");
    if (!data.companyName) throw new Error("企业名称不能为空");
    if (!data.jobType) throw new Error("工作类型不能为空");
    if (!data.companyEmail) throw new Error("企业邮箱不能为空");
    if (!data.contactPerson) throw new Error("联系人不能为空");
    if (!data.contactPhone) throw new Error("联系电话不能为空");
    if (!data.workLocation) throw new Error("工作地点不能为空");
    try {
      const response = await post("/company-jobs", data, true);
      return response.data || response;
    } catch (error) {
      console.error("Error creating company job:", error);
      throw new Error("创建招聘信息失败");
    }
  },

  async updateCompanyJob(id, data) {
    if (!id) throw new Error("招聘信息ID不能为空");
    if (!data.jobTitle) throw new Error("招聘岗位不能为空");
    if (!data.companyName) throw new Error("企业名称不能为空");
    if (!data.jobType) throw new Error("工作类型不能为空");
    if (!data.companyEmail) throw new Error("企业邮箱不能为空");
    if (!data.contactPerson) throw new Error("联系人不能为空");
    if (!data.contactPhone) throw new Error("联系电话不能为空");
    if (!data.workLocation) throw new Error("工作地点不能为空");
    try {
      const response = await put(`/company-jobs/${id}`, data, true);
      return response.data || response;
    } catch (error) {
      console.error("Error updating company job:", error);
      throw new Error("更新招聘信息失败");
    }
  },

  async deleteCompanyJob(id) {
    if (!id) throw new Error("招聘信息ID不能为空");
    try {
      const response = await del(`/company-jobs/${id}`);
      return response.data || response;
    } catch (error) {
      console.error("Error deleting company job:", error);
      throw new Error("删除招聘信息失败");
    }
  },

  async batchDeleteCompanyJobs(ids) {
    if (!ids || ids.length === 0) throw new Error("请选择要删除的招聘信息");
    try {
      const deletePromises = ids.map((id) => this.deleteCompanyJob(id));
      await Promise.all(deletePromises);
      return { success: true };
    } catch (error) {
      console.error("Error batch deleting company jobs:", error);
      throw new Error("批量删除招聘信息失败");
    }
  },
};
